/*    
 * Swing Frame GUI for Google Desktop Translator
 * Copyright (C) 2009   Dusan Misic / MD Systems
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.google.desktop.translator.ui;

import com.google.desktop.translator.engine.TranslationEngine;
import java.io.IOException;
import javax.swing.JOptionPane;

/**
 *
 * @author  Dusan
 */
public class MainJFrame extends javax.swing.JFrame {

    /** Creates new form MainJFrame */
    public MainJFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        inputLanguageJComboBox = new javax.swing.JComboBox();
        outputLanguageJComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        inputJTextArea = new javax.swing.JTextArea();
        aboutJButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        outputJTextArea = new javax.swing.JTextArea();
        translateJButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Google Desktop Translator 0.2");
        setLocationByPlatform(true);
        setMinimumSize(new java.awt.Dimension(635, 430));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel3.setText("Input language:");

        jLabel4.setText("Output language:");

        jLabel2.setText("Input text:");

        inputJTextArea.setColumns(20);
        inputJTextArea.setLineWrap(true);
        inputJTextArea.setRows(5);
        inputJTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                inputJTextAreaKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(inputJTextArea);

        aboutJButton.setText("About ...");
        aboutJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutJButtonActionPerformed(evt);
            }
        });

        jLabel5.setText("Output text:");

        outputJTextArea.setColumns(20);
        outputJTextArea.setLineWrap(true);
        outputJTextArea.setRows(5);
        jScrollPane2.setViewportView(outputJTextArea);

        translateJButton.setText("Translate");
        translateJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                translateJButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 413, Short.MAX_VALUE)
                        .addComponent(jLabel4))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(inputLanguageJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 292, Short.MAX_VALUE)
                        .addComponent(outputLanguageJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 365, Short.MAX_VALUE)
                        .addComponent(translateJButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(aboutJButton))
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(outputLanguageJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(inputLanguageJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(aboutJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(translateJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
// TODO add your handling code here:

    // Loading JComboBoxes
    // Loading Input ComboBox
    TranslationEngine inputLanguage = new TranslationEngine();
    for (int counter = 0; counter < inputLanguage.getLanguageNumber(); counter++) {
        this.inputLanguageJComboBox.addItem(inputLanguage.getTranslationLanguage(counter));
    }

    TranslationEngine outputLanguage = new TranslationEngine();
    for (int counter = 0; counter < outputLanguage.getLanguageNumber(); counter++) {
        this.outputLanguageJComboBox.addItem(outputLanguage.getTranslationLanguage(counter));
    }
    
    // Disable Translate button
    this.translateJButton.setEnabled(false);
}//GEN-LAST:event_formWindowOpened

private void inputJTextAreaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_inputJTextAreaKeyTyped
// TODO add your handling code here:

    if (inputJTextArea.getDocument().getLength() == 0) {
        this.translateJButton.setEnabled(false);
    } else if (inputJTextArea.getDocument().getLength() > 0) {
        this.translateJButton.setEnabled(true);
    }

}//GEN-LAST:event_inputJTextAreaKeyTyped

private void aboutJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutJButtonActionPerformed
// TODO add your handling code here:
    String aboutMessage = "Google Desktop Translator 0.2 Kestrel\n\n" +
            "Copyright: Dusan Misic / MD Systems <promisic@gmail.com>\n\n"
            + "Google Translation API for Java:\n\n" +
            "Richard Midwinter (http://code.google.com/p/google-api-translate-java/)";
    JOptionPane.showMessageDialog(rootPane, aboutMessage, "About ....", JOptionPane.INFORMATION_MESSAGE);
}//GEN-LAST:event_aboutJButtonActionPerformed

private void translateJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_translateJButtonActionPerformed
// TODO add your handling code here:
    this.outputJTextArea.setText("");
    
    String textToTranslate = null;
    String translatedText = null;
    
    String inputLanguage = String.valueOf(this.inputLanguageJComboBox.getSelectedItem());
    String outputLanguage = String.valueOf(this.outputLanguageJComboBox.getSelectedItem());
    
    TranslationEngine translateText = new TranslationEngine();
    try {
           textToTranslate = this.inputJTextArea.getText();
           translatedText = translateText.translateMagic(textToTranslate, inputLanguage, outputLanguage);
           this.outputJTextArea.setText(translatedText);
    } catch (ClassNotFoundException ex) {
        JOptionPane.showMessageDialog(rootPane, "Google API library not available.", "CRITICAL ERROR!", JOptionPane.ERROR_MESSAGE);
    } catch (IOException ex) {
         JOptionPane.showMessageDialog(rootPane, "Can't communicate to Google translation service.\n"
                 + "Maybe you are not connected to the Internet?", "Communication Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_translateJButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aboutJButton;
    private javax.swing.JTextArea inputJTextArea;
    private javax.swing.JComboBox inputLanguageJComboBox;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea outputJTextArea;
    private javax.swing.JComboBox outputLanguageJComboBox;
    private javax.swing.JButton translateJButton;
    // End of variables declaration//GEN-END:variables
}
